package final2107.day04;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ProductorBlocking {
    public static void main(String[] args) {
        //创建容量为5的阻塞队列
        BlockingQueue<String> queue =
                new LinkedBlockingQueue<>(5);
        //创建生产者线程
        Thread produtor = new Thread(){
            @Override
            public void run() {
                for (int i=0;i<100;i++){
                    try {
                        queue.put("hello"+i);
                        System.out.println(queue);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        //创建消费者线程 - 每隔2s从队列中取出数据
        Thread consumer = new Thread(){
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(2000);
                        queue.take();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        produtor.start();
        consumer.start();

    }
}
